package com.hbusa.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author XiaoXin
 * @date 2021/12/25 - 0:26
 */

@WebFilter("/*")
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setCharacterEncoding("utf-8");
        request.setCharacterEncoding("utf-8");
        String uri = request.getRequestURI();
        System.out.println(uri);
        if (uri.contains("/static")) {
            System.out.println("静态资源放过");
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (uri.equals("/admin/choose.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doLogin")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doUpload")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doList")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doAdd")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doDel")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("pictureAdd")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("image.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doInfo")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("search.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }

        if (uri.contains("doUpdate")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doSearch")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("chapter.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doNovelChapter")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doRead")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doNext")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("read.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        //jsp
        if (uri.contains("novelAdd.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("novelFindAll.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("novelModify.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("chapterModify.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("chapterFindAll.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("chapterAdd.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("categoryAdd.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("categoryFindAll.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("categoryModify.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }


        //servlet
        if (uri.contains("/doNovelAdd")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doNovelDelete")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doNovelFindAll")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doNovelModify")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doChapterFindAll")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doChapterModify")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/jspServlet")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doChapterAdd")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("/doChapterDelete")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doCategoryAdd")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doCategoryDelete")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doCategoryFindAll")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        if (uri.contains("doCategoryModify")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }



        HttpSession session = request.getSession();
        String username = (String)session.getAttribute("username");
        System.out.println("filter->username:" + username);
        if (uri.equals("/index1")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (uri.equals("/index.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        if (username == null && uri.contains("login.jsp")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return ;
        }
        request.getRequestDispatcher("/admin/choose.jsp").forward(request, response);
    }

    @Override
    public void destroy() {
    }
}
